package com.xxxx;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Hello02OperatorChain {

    public static void main(String[] args) throws Exception {
        // 设置执行环境, 类似spark中初始化sparkContext一样
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 关闭操作链..
        //env.disableOperatorChaining();
        DataStreamSource<String> source = env.fromElements("how", "are", "you", "1", "2", "3", "4", "5", "6", "7", "8");
        SingleOutputStreamOperator<String> map = source.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                System.err.println(value + " ---- 1");
                return value;
            }
        }).setParallelism(4).map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                System.err.println(value + " ---- 2");
                return value;
            }
        }).setParallelism(8);
        // 还可以控制更细粒度的任务链,比如指明从哪个operator开始形成一条新的链
        // someStream.map(...).startNewChain()，但不能使用someStream.startNewChain()。
        env.execute();
    }
}